package com.taiyi.renaissancemall.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.constraintlayout.widget.ConstraintLayout;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.AgreementsBean;
import com.taiyi.renaissancemall.ui.webview.WebViewActivity;


import butterknife.BindView;

import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterView, RegisterPresent> implements RegisterView {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.verificationCodeEt)
    EditText verificationCodeEt;
    @BindView(R.id.getVerificationCodeTv)
    TextView getVerificationCodeTv;
    @BindView(R.id.inviteCodeEt)
    EditText inviteCodeEt;
    @BindView(R.id.isAgreeCheckBox)
    CheckBox isAgreeCheckBox;
    @BindView(R.id.tvAgreeCheckBox)
    TextView tvAgreeCheckBox;
    private TimeCount timeCount;
    @BindView(R.id.top)
    ConstraintLayout base_top_layout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterPresent initPresenter() {
        return new RegisterPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.register));
        timeCount = new TimeCount(60000, 1000);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(base_top_layout).init();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initEvent() {

    }


    @OnClick({R.id.backIv, R.id.getVerificationCodeTv, R.id.toLoginTv, R.id.registerTv, R.id.tvAgreeCheckBox})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.getVerificationCodeTv:
                presenter.getVerificationCode();
                break;
            case R.id.toLoginTv:
                finish();
                break;
            case R.id.tvAgreeCheckBox:
                //跳转注册协议页面
                Intent intent = new Intent(RegisterActivity.this, WebViewActivity.class);
                intent.putExtra("isWhere", 1);
                startActivity(intent);
                break;
            case R.id.registerTv:
                if (TextUtils.isEmpty(phoneNumberEt.getText().toString())) {
                    ToastUtils.showShortToast("请填写手机号");
                    return;
                }
                if (!RegexUtil.checkPhone(phoneNumberEt.getText().toString())) {
                    ToastUtils.showShortToast("请填写正确手机号");
                    return;
                }
                if (TextUtils.isEmpty(verificationCodeEt.getText().toString())) {
                    ToastUtils.showShortToast("请填写验证码");
                    return;
                }
                if (TextUtils.isEmpty(inviteCodeEt.getText().toString())) {
                    ToastUtils.showShortToast("请填写邀请码");
                    return;
                }
                if (!isAgreeCheckBox.isChecked()) {
                    ToastUtils.showShortToast("请勾选注册协议");
                    return;
                }
                presenter.register();
                break;
        }
    }

    class TimeCount extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getVerificationCodeTv.setEnabled(false);
            long time = millisUntilFinished % 1000 > 500 ? millisUntilFinished / 1000 + 1 : millisUntilFinished / 1000;
            getVerificationCodeTv.setText(String.valueOf(time + "秒"));
        }

        @Override
        public void onFinish() {
            getVerificationCodeTv.setText("重新获取");
            getVerificationCodeTv.setEnabled(true);
        }
    }

    @Override
    public String getPhoneNumber() {
        return TextUtils.isEmpty(phoneNumberEt.getText().toString()) ? "" : phoneNumberEt.getText().toString();
    }

    @Override
    public String getVerificationCode() {
        return TextUtils.isEmpty(verificationCodeEt.getText().toString()) ? "" : verificationCodeEt.getText().toString();
    }

    @Override
    public String getInviteCode() {
        return TextUtils.isEmpty(inviteCodeEt.getText().toString()) ? "" : inviteCodeEt.getText().toString();
    }

    @Override
    public void onSuccess() {
        hideProgress();
        timeCount.start();
    }

    @Override
    public void onFailure(String msg) {
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void registerSuccess() {
        finish();
    }

}
